package com.woniuxy.table.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.table.common.TableResultCode;
import com.woniuxy.table.infra.mapper.DinnerTableMapper;
import com.woniuxy.table.infra.model.DiningArea;
import com.woniuxy.table.infra.model.DinnerTable;
import com.woniuxy.table.infra.model.DinnerTableType;
import com.woniuxy.table.service.DiningAreaService;
import com.woniuxy.table.service.DinnerTableService;
import com.woniuxy.table.service.DinnerTableTypeService;
import com.woniuxy.table.service.dto.DinnerTableDto;
import com.woniuxy.table.service.param.AddTableServiceParam;
import com.woniuxy.table.service.param.LoadAllStoreTableServiceParam;
import com.woniuxy.table.service.param.ModifyTableServiceParam;
import com.woniuxy.table.service.util.OSSUtil;
import com.woniuxy.table.service.util.ZxingUtil;
import common.WoniuPage;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 餐桌信息表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-20
 */
@Service
public class DinnerTableServiceImpl extends ServiceImpl<DinnerTableMapper, DinnerTable> implements DinnerTableService {
    @Autowired
    private DinnerTableMapper dinnerTableMapper;
    @Autowired
    private DiningAreaService diningAreaService;
    @Autowired
    private DinnerTableTypeService dinnerTableTypeService;
    @Autowired
    private OSSUtil ossUtil;

    @Override
    public void addTable(AddTableServiceParam addTableServiceParam){
        QueryWrapper<DinnerTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_num",addTableServiceParam.getTableNum()).eq("store_id",addTableServiceParam.getStoreId());
        DinnerTable dinnerTable = dinnerTableMapper.selectOne(queryWrapper);
        if (dinnerTable!=null){
            throw new MallException(TableResultCode.TABLE_NUMBER_DUPLICATED);
        }
        DiningArea diningArea = diningAreaService.getById(addTableServiceParam.getDiningAreaId());
        if (diningArea==null||diningArea.getStoreId()!=addTableServiceParam.getStoreId()){
            throw new MallException(TableResultCode.INVALID_REGIONAL_ADDRESS);
        }
        DinnerTableType dinnerTableType = dinnerTableTypeService.getById(addTableServiceParam.getTableTypeId());
        if (dinnerTableType==null||dinnerTableType.getStoreId()!=addTableServiceParam.getStoreId()){
            throw new MallException(TableResultCode.TYPE_DOES_NOT_EXIST);
        }
        DinnerTable newDinnerTable = new DinnerTable();
        newDinnerTable.setTableNum(addTableServiceParam.getTableNum());
        newDinnerTable.setTableState("FREE");
        newDinnerTable.setTableTypeId(addTableServiceParam.getTableTypeId());
        newDinnerTable.setDiningAreaId(addTableServiceParam.getDiningAreaId());
        newDinnerTable.setStoreId(addTableServiceParam.getStoreId());
        newDinnerTable.setMaxSite(dinnerTableType.getMaxSite());
        String qrCode = null;
        try {
            qrCode = GenerateQRCode(addTableServiceParam.getStoreId(), addTableServiceParam.getTableNum());
        } catch (Exception e) {
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        newDinnerTable.setQrCode(qrCode);
        save(newDinnerTable);
    }

    @Override
    public void modifyTable(ModifyTableServiceParam modifyTableServiceParam) throws Exception {
        DinnerTable table = getById(modifyTableServiceParam.getTableId());
        if (table==null){
            throw new MallException(TableResultCode.DINING_TABLE_DOES_NOT_EXIST);
        }
        QueryWrapper<DinnerTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_num",modifyTableServiceParam.getTableName()).eq("store_id",modifyTableServiceParam.getStoreId());
        DinnerTable dinnerTable = dinnerTableMapper.selectOne(queryWrapper);
        if (dinnerTable!=null) {
            throw new MallException(TableResultCode.TABLE_NUMBER_DUPLICATED);
        }

        if (modifyTableServiceParam.getTableState()!=null&&!modifyTableServiceParam.equals("")){
            if (table.getTableState().equals(modifyTableServiceParam.getTableState())) {
                throw new MallException(TableResultCode.IS_ALREADY_IN_THIS_STATE);
            }
            table.setTableState(modifyTableServiceParam.getTableState());
        }

        if (modifyTableServiceParam.getTableName()!=null&&!modifyTableServiceParam.getTableName().equals("")){
            table.setTableNum(modifyTableServiceParam.getTableName());
            if (table.getQrCode()!=null){
                String name = table.getQrCode().substring(table.getQrCode().lastIndexOf("/"));
                ossUtil.deleteFile("woniu95",name.substring(1));
            }
            System.out.println(modifyTableServiceParam.getTableName());
            String s = GenerateQRCode(modifyTableServiceParam.getStoreId(), modifyTableServiceParam.getTableName());
            table.setQrCode(s);
        }

        updateById(table);
    }

    @Override
    public void deleteTable(Integer storeId, Integer tableId) {
        DinnerTable table = getById(tableId);
        if (table==null){
            throw new MallException(TableResultCode.DINING_TABLE_DOES_NOT_EXIST);
        }
        if (table.getStoreId()!=storeId){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        removeById(tableId);
    }

    @Override
    public WoniuPage<DinnerTableDto> loadAllStoreTable(LoadAllStoreTableServiceParam loadAllStoreTableServiceParam) {
        QueryWrapper<DinnerTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",loadAllStoreTableServiceParam.getStoreId());
        if (loadAllStoreTableServiceParam.getTableState()!=null&&!loadAllStoreTableServiceParam.getTableState().equals("")){
            queryWrapper.eq("table_state",loadAllStoreTableServiceParam.getTableState());
        }
        if (loadAllStoreTableServiceParam.getTableTypeId()!=null){
            queryWrapper.eq("table_type_id",loadAllStoreTableServiceParam.getTableTypeId());
        }
        if (loadAllStoreTableServiceParam.getDiningAreaId()!=null){
            queryWrapper.eq("dining_area_id",loadAllStoreTableServiceParam.getDiningAreaId());
        }
        Page<DinnerTable> dinnerTablePage = new Page<>(loadAllStoreTableServiceParam.getPageIndex(), loadAllStoreTableServiceParam.getPageSize());
        dinnerTableMapper.selectPage(dinnerTablePage,queryWrapper);
        WoniuPage<DinnerTableDto> woniuPage = new WoniuPage<>();
        woniuPage.setTotal(dinnerTablePage.getTotal());
        woniuPage.setPages(dinnerTablePage.getPages());
        woniuPage.setPageIndex(dinnerTablePage.getCurrent());
        woniuPage.setPageSize(dinnerTablePage.getSize());
        ArrayList<DinnerTableDto> list = new ArrayList<>();
        for (DinnerTable dinnerTable : dinnerTablePage.getRecords()) {
            DinnerTableDto dinnerTableDto = BeanUtil.toBean(dinnerTable, DinnerTableDto.class);
            DinnerTableType tableType = dinnerTableTypeService.getById(dinnerTable.getTableTypeId());
            dinnerTableDto.setTableTypeName(tableType.getTableTypeName());
            DiningArea diningArea = diningAreaService.getById(dinnerTable.getDiningAreaId());
            dinnerTableDto.setDiningAreaName(diningArea.getDiningAreaName());
            list.add(dinnerTableDto);
        }
        woniuPage.setList(list);
        return woniuPage;
    }

    @Override
    public List<DinnerTableDto> getStoreTableByStoreId(Integer storeId) {
        QueryWrapper<DinnerTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id",storeId);
        List<DinnerTable> dinnerTables = dinnerTableMapper.selectList(queryWrapper);
        List<DinnerTableDto> dinnerTableDtos = BeanUtil.copyToList(dinnerTables, DinnerTableDto.class);
        return dinnerTableDtos;
    }

    public static String GenerateQRCode(Integer storeId, String tableNum) throws Exception {
        String text = "storeId:"+storeId+",tableNum:"+tableNum+"";
        System.out.println(text);
        ZxingUtil zxingUtil = new ZxingUtil();
        String imge = zxingUtil.createImge(text);
        return imge;
    }
}
